package cn.kgc.rainmanager.Controller;

import cn.kgc.rainmanager.pojo.AirQI;
import cn.kgc.rainmanager.pojo.ResultEntity;
import cn.kgc.rainmanager.service.AirService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class AirController {
    @Resource
    AirService airService;

    @RequestMapping("/selectAir")
    public ResultEntity<PageInfo<AirQI>> selectAir(
            @RequestParam(value = "districtId", required = false, defaultValue = "0") Integer districtId,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        PageInfo<AirQI> pageInfo = airService.select(pageNum, pageSize, districtId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @RequestMapping("/saveAir")
    public ResultEntity<String> saveAir(@RequestBody AirQI air) {
        try {
            Integer integer = 0;
            if (air.getId() == null) {
                //添加
                integer = airService.add(air);
            } else {
                //删除
                integer = airService.update(air);
            }
            return ResultEntity.successWithoutData(integer+"");
        } catch (Exception ex) {
            return ResultEntity.failed(ex.getMessage());
        }
    }
        //根据id查询图书
        @RequestMapping("/getAirById")
        public ResultEntity<AirQI> getAirId (Integer id){
            AirQI entry = airService.selectById(id);
            return ResultEntity.successWithoutData(entry);
        }

        //根据id查询图书
        @RequestMapping("/delAirById")
        public ResultEntity<String> delEntryById (Integer id){
            try {
            Integer integer = airService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
            } catch (Exception ex) {
                return ResultEntity.failed(ex.getMessage());
            }
        }

}
